package com.epfresh.supplier.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RelativeLayout;

import com.epfresh.api.entity.RequestEntitySupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.global.BaseApplication;
import com.epfresh.api.http.HttpRequest;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.universalimageloader.core.DisplayImageOptions;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.universalimageloader.core.ImageLoaderConfiguration;
import com.epfresh.api.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.epfresh.api.universalimageloader.utils.Log;
import com.epfresh.api.utils.AppUtils;
import com.epfresh.api.widget.NoScrollViewPager;
import com.epfresh.api.widget.OnlyRadioGroup;
import com.epfresh.api.widget.circleprogress.CircleProgress;
import com.epfresh.api.widget.tablayout.CommonTabLayout;
import com.epfresh.api.widget.tablayout.TabEntity;
import com.epfresh.api.widget.tablayout.listener.OnTabSelectListener;
import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.EvaluateSumEntity;
import com.epfresh.supplier.fragment.EvaluatesList;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class MoreEvaluateActivity extends BaseActivity {
	private NoScrollViewPager mViewPager;
	private final int TAB_COUNT = 4;
	int[] numbers;

	private OnlyRadioGroup mCheckLayout;
	private RadioButton allRadioButton;
	private RadioButton goodRadioButton;
	private RadioButton middleRadioButton;
	private RadioButton badRadioButton;
	Context mContext;
	String level="";
	public static String id="";
	int page=0;
	static ImageLoader imageLoader;
	static DisplayImageOptions options;


	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_evaluate_more);
		mContext = this;
		initView();
		initProgress();
		imageLoader = ImageLoader.getInstance();
		imageLoader.init(ImageLoaderConfiguration.createDefault(mContext));
		options = new DisplayImageOptions.Builder().showStubImage(R.mipmap.demo)
				.showImageForEmptyUri(R.mipmap.demo).showImageOnFail(R.mipmap.demo).cacheInMemory()
				.cacheOnDisc().displayer(new RoundedBitmapDisplayer(360))
				.build();
		mHttpLoad(page);
		//bitmap = new BitmapUtils(this);
		//basetitle.setText("获得的评价");
		basetitle.setText("评价");
		baseback.setVisibility(View.VISIBLE);
		mViewPager = (NoScrollViewPager) findViewById(R.id.evaluate_more_viewpager);
		mViewPager.setSlidingEnable(false);
		//mTabLayout = (CommonTabLayout) findViewById(R.id.id_tablayout);
		//mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
	}

	CircleProgress mEvaluatePercentPb;
	private void initProgress() {
		mEvaluatePercentPb = (CircleProgress) findViewById(R.id.evaluate_more_progress);
//		String stringPercent = getIntent().getStringExtra("evaluate_percent");
//		Log.i("tony","---stringPercent--->>>"+stringPercent);
		int[] mGradientColors = {getResources().getColor(R.color.color_00cfc3), getResources().getColor(R.color.color_3eb049)};
		mEvaluatePercentPb.setGradientColors(mGradientColors);
/*		float parseInt = 0;
		if (stringPercent != null) {
			parseInt = Float.parseFloat(stringPercent);
		}
		mEvaluatePercentPb.setValue(parseInt);*/
	}

	private void initView() {
		mCheckLayout = (OnlyRadioGroup) findViewById(R.id.evaluate_more_comments_layout);
		mCheckLayout.setOnCheckedChangeListener(mOnCheckedChangeListener);
		allRadioButton = (RadioButton) findViewById(R.id.evaluate_more_all_comment_checkbox);
		goodRadioButton = (RadioButton) findViewById(R.id.evaluate_more_good_comment_checkbox);
		middleRadioButton = (RadioButton) findViewById(R.id.evaluate_more_middle_comment_checkbox);
		badRadioButton = (RadioButton) findViewById(R.id.evaluate_more_bad_comment_checkbox);
	}

	OnlyRadioGroup.OnCheckedChangeListener mOnCheckedChangeListener = new OnlyRadioGroup.OnCheckedChangeListener() {
		@Override
		public void onCheckedChanged(OnlyRadioGroup group, int checkedId) {
			switch (checkedId) {
				case R.id.evaluate_more_all_comment_checkbox:
					mViewPager.setCurrentItem(0,false);
					break;
				case R.id.evaluate_more_good_comment_checkbox:
					mViewPager.setCurrentItem(1,false);
					break;
				case R.id.evaluate_more_middle_comment_checkbox:
					mViewPager.setCurrentItem(2,false);
					break;
				case R.id.evaluate_more_bad_comment_checkbox:
					mViewPager.setCurrentItem(3,false);
					break;
			}
		}
	};

	public void setAdapter() {
		mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
			@Override
			public Fragment getItem(int i) {
				return new EvaluatesList(i);
			}

			@Override
			public CharSequence getPageTitle(int position) {
				return "";
			}

			@Override
			public int getCount() {
				return TAB_COUNT;
			}
		});

		String[] mTitles = {"全部\n" + numbers[0],

				"好评\n" + numbers[1],"中评\n" + numbers[2], "差评\n" + numbers[3]
		};
		ArrayList<TabEntity> mTabEntities = new ArrayList<>();
		for (int i = 0; i < mTitles.length; i++) {
			mTabEntities.add(new TabEntity(mTitles[i], 0, 0));
		}
	}
	public void mHttpLoad(int page) {
		//String token = getUser().getToken();
		String uuid = AppUtils.getUniquePsuedoID();
		RequestEntitySupply<Map<String, String>> entity = new RequestEntitySupply<>();
		Map<String, String> map = new HashMap<>();
		map.put("level", level);
		map.put("pageNumber", page + "");
		map.put("pageSize", 10 + "");
		map.put("sortProperty", "time");
		map.put("sortDirection", "DESC");
		map.put("storeId", BaseApplication.getInstance().getStoreId());
		entity.setCmd(com.epfresh.api.constant.AppSupplyRouterConstant.cmd_store_store_comment_list4store);
		//entity.setToken(token);
		entity.setClientIdentifierCode(uuid);
		entity.setParameters(map);
		request(entity, com.epfresh.api.constant.AppSupplyRouterConstant.cmd_store_store_comment_list4store, onRequestListener);
	}

	OnRequestListener<EvaluateSumEntity> onRequestListener = new OnRequestListener<EvaluateSumEntity>() {
		@Override
		public EvaluateSumEntity jsonToObj(String responseStr) {
			return new Gson().fromJson(responseStr, EvaluateSumEntity.class);
		}

		@Override
		public void onFail(int failCode, Object arg, Object tag) {
			switch (failCode) {
				case HttpRequest.FAIL_NOT_FOUND:

					break;
				case HttpRequest.FAIL_NO_NET:

					break;
				case HttpRequest.FAIL_NET_FAULT:

				default:

					break;
			}
		}

		@Override
		public void onResponseError(int failCode, Object arg, Object tag) {
		}

		@Override
		public void onStart(Object tag) {

		}

		@Override
		public void onResponse(ResponseEntity<EvaluateSumEntity> response, Object tag) {
			EvaluateSumEntity orderResponse = response.getResponseElement();
			numbers = new int[]{
					orderResponse.getAllCommentsCount(), orderResponse.getGoodCommentsCount(),
                    orderResponse.getMiddleCommentsCount(), orderResponse.getBadCommentsCount()
			};
			allRadioButton.setText("全部" + "(" + numbers[0] + ")");
			goodRadioButton.setText("点赞" + "(" + numbers[1] + ")");
			middleRadioButton.setText("满意" + "(" + numbers[2] + ")");
			badRadioButton.setText("吐槽" + "(" + numbers[3] + ")");
			if(orderResponse.getGoodCommentsRate()!=null&&orderResponse.getGoodCommentsRate()!=0) {
				mEvaluatePercentPb.setValue(orderResponse.getGoodCommentsRate());
			}else {
				int[] mGradientColors = {Color.parseColor("#00ffffff"), Color.parseColor("#00ffffff")};
				mEvaluatePercentPb.setGradientColors(mGradientColors);
				mEvaluatePercentPb.setValue(0);
			}
			setAdapter();


		}
	};
}
